﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Linq;
using Matrix_network.Code.EntityClasses;

namespace Matrix_network.Code
{
    public class MessagesDataSource
    {
        private int shortMessageLength = 50;
        MatrixDataClassesDataContext context = new MatrixDataClassesDataContext();
        public MessagesDataSource()
        {
 
        }
        
       
        /// <summary>
        /// Get users name and surname from id
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public string getNameFromId(int id)
        {
            string name = "";
            ISingleResult<get_user_name_by_idResult> userRes
                    = context.get_user_name_by_id(id);
            foreach (get_user_name_by_idResult uRes in userRes)
            {
                name = uRes.name + "\n " + uRes.surname;
            }
            return name;
        }

        /// <summary>
        /// Get messages List <MessageInfo>
        /// </summary>
        /// <param name="id"></param>
        /// <param name="maximumRows"></param>
        /// <param name="startRowIndex"></param>
        /// <returns></returns>
        public List<MessageInfo> GetMessagesList(int id, int maximumRows, int startRowIndex)
        {
            ISingleResult<get_messages_listResult> msgRes
            = context.get_messages_list(id);
            List<MessageInfo> msgList = new List<MessageInfo>();
            string name = "";
            foreach (get_messages_listResult res in msgRes)
            {
                MessageInfo mInfo = new MessageInfo();
                ISingleResult<get_ava_idResult> ava
                    = context.get_ava_id(res.from_id);
                foreach (get_ava_idResult r in ava)
                {
                    if (r.ava_id!=null)
                        mInfo.avaId = (int) r.ava_id;
                }
                name = getNameFromId(res.from_id);
                mInfo.from = name;
                mInfo.time = res.date_time;
                if (res.msg.Length > shortMessageLength)
                {
                    mInfo.message = res.msg.Substring(0, shortMessageLength) + "...";
                }
                else 
                {
                    mInfo.message = res.msg;
                }
                mInfo.id = (int) res.id;
                mInfo.fromId = res.from_id;
                mInfo.toId = res.usr_id;
                msgList.Add(mInfo);
            }
            if (startRowIndex > msgList.Count)
                return null;
            List<MessageInfo> handledList = new List<MessageInfo>();
            int current = startRowIndex;
            while (current < msgList.Count && current < startRowIndex + maximumRows)
            {
                handledList.Add(msgList[current]);
                current++;
            }
            return handledList;
        }


        /// <summary>
        /// Get sended messages List <MessageInfo>
        /// </summary>
        /// <param name="id"></param>
        /// <param name="maximumRows"></param>
        /// <param name="startRowIndex"></param>
        /// <returns></returns>
        public List<MessageInfo> GetMessagesOutList(int id, int maximumRows, int startRowIndex)
        {
            ISingleResult<get_messages_out_listResult> msgRes
            = context.get_messages_out_list(id);
            List<MessageInfo> msgList = new List<MessageInfo>();
            string name = "";
            foreach (get_messages_out_listResult res in msgRes)
            {
                MessageInfo mInfo = new MessageInfo();
                ISingleResult<get_ava_idResult> ava
                    = context.get_ava_id(res.usr_id);
                foreach (get_ava_idResult r in ava)
                {
                    if (r.ava_id != null)
                        mInfo.avaId = (int)r.ava_id;
                }
                name = getNameFromId(res.usr_id);
                mInfo.from = name;
                mInfo.time = res.date_time;
                if (res.msg.Length > shortMessageLength)
                {
                    mInfo.message = res.msg.Substring(0, shortMessageLength) + "...";
                }
                else
                {
                    mInfo.message = res.msg;
                }
                mInfo.id = (int)res.id;
                mInfo.fromId = res.from_id;
                mInfo.toId = res.usr_id;
                
                msgList.Add(mInfo);
            }
            if (startRowIndex > msgList.Count)
                return null;
            List<MessageInfo> handledList = new List<MessageInfo>();
            int current = startRowIndex;
            while (current < msgList.Count && current < startRowIndex + maximumRows)
            {
                handledList.Add(msgList[current]);
                current++;
            }
            return handledList;
        }

        /// <summary>
        /// Count of messages 
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int GetCount(int id)
        {
            ISingleResult<get_messages_listResult> msg_res
            = context.get_messages_list(id);
            return msg_res.Count();
            
        }

        /// <summary>
        /// Count of sended messages
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int GetOutCount(int id)
        {
            ISingleResult<get_messages_out_listResult> msg_res
            = context.get_messages_out_list(id);
            return msg_res.Count();
        }

        /// <summary>
        /// Get income message
        /// </summary>
        /// <param name="id"></param>
        /// <param name="userId"></param>
        /// <returns></returns>
        public MessageInfo getInMessage(int id,int userId)
        {
            ISingleResult<get_message_from_idResult> msgRes
            = context.get_message_from_id(id);
            string msg = "";
            MessageInfo mi = new MessageInfo();
            int msgUserId=-1;
            foreach (get_message_from_idResult uRes in msgRes)
            {
                ISingleResult<get_ava_idResult> ava
                    = context.get_ava_id(uRes.from_id);
                foreach (get_ava_idResult r in ava)
                {
                    if (r.ava_id != null)
                        mi.avaId = (int)r.ava_id;
                }
                mi.message = uRes.msg;
                msgUserId = uRes.usr_id;
                mi.time = uRes.date_time;
                mi.fromId = uRes.from_id;
            }
            if (userId != msgUserId)
            {
                return null;
            }
            return mi;
        }

        /// <summary>
        /// Get sended message
        /// </summary>
        /// <param name="id">msg_id</param>
        /// <param name="userId"></param>
        /// <returns></returns>
        public MessageInfo getOutMessage(int id, int userId)
        {
            ISingleResult<get_message_from_idResult> msgRes
            = context.get_message_from_id(id);
            string msg = "";
            int msgUserId = -1;
            MessageInfo mi = new MessageInfo();
            foreach (get_message_from_idResult uRes in msgRes)
            {
                ISingleResult<get_ava_idResult> ava
                    = context.get_ava_id(uRes.usr_id);
                foreach (get_ava_idResult r in ava)
                {
                    if (r.ava_id != null)
                        mi.avaId = (int)r.ava_id;
                }
                mi.message = uRes.msg;
                msgUserId = uRes.from_id;
                mi.fromId = uRes.from_id;
                mi.toId = uRes.usr_id;
                mi.time = uRes.date_time;
            }
            if (userId != msgUserId)
            {
                return null;
            }
            return mi;
        }

        /// <summary>
        /// Sent message
        /// </summary>
        /// <param name="id"></param>
        /// <param name="toId"></param>
        /// <param name="message"></param>
        public void sentMessage(int id, int toId,string message)
        {
            context.add_message(id,toId,DateTime.Now,message);
        }
    }
}